<?php
class login extends Controller {
	public function html() {
		$mUser = new User_model();
		$data = array();
		$data['backurl'] = $this->backurl()? $this->backurl(): '/';
		if($mUser->currUser()) header('Location:'.$data['backurl']);
		$this->display($data, 'auth/login.html');
	}
	public function json() {
		$mUser = new User_model();
		$user = $mUser->currUser();
		if($user) $this->displayJSON(array('status' => 0, 'message' => '登录成功', 'user' => $user));

		$username = $this->post('username', 'str');
		$password = $this->post('password', 'str');
		
		if(!$username) $this->displayJSON(array('status' => -1, 'message' => '请输入账号'));
		if(!$password) $this->displayJSON(array('status' => -2, 'message' => '请输入密码'));

		$result = $mUser->login($username, $password);
		if(is_array($result) && !empty($result)) {
			//写日志
			$mLog = new Log_model();
			$id = $mLog->write($result['uid'], '登陆系统');
			$this->displayJSON(array('status' => 0, 'message' => '登录成功', 'user' => $result));
		}
		switch ($result) {
			case -3:
				$message = '账号不存在';
				break;
			case -4:
				$message = '账号或密码错误';
				break;
			case -5:
				$message = '此账号已失效';
				break;
			default:
				$result = 500;
				$message = '系统繁忙，请稍后登录';
				break;
		};
		
		$this->displayJSON(array('status' => $result, 'message' => $message));
	}
}